<?php
include 'core/initialize.php';
logged_in_redirect();



if(empty ($_POST) === false){
    $username = $_POST ['username'] ;
    $password = $_POST ['password'] ;
    
    if(empty($username) === true || empty ($password) === true) {
        $errors[] = ' Enter username and password';
    } else if (user_exists($username) === false ) {
        $errors[] = ' Can\'t find the username. Have you registered?';
    } else {
        
        if (strlen ($password)>32 ){
            $errors[] = 'Password is too long';
        }
        $login = login($username, $password);
        if($login === false) {
            $errors[] = 'That username/password combination is incorrect';
        } else {
           $_SESSION ['ID'] = $login;
           header('Location: index.php');
           exit();
        }
    }
} else{
    $errors[] = ' No Data Received'; 
}
include 'include/overall/Overallheader.php';

if (empty($errors) === false) {
    ?>
    <h2> We tried to log you in but.. <br><br></h2>
    <?php
    echo output_errors($errors);
}

include 'include/overall/Overallfooter.php';
?>